<style lang="less">
@import "../../styles/common.less";
</style>

<template>
  <div>
    <Card :bordered="false">
		<p slot="title">支付订单查询</p>
		<p>
			<RadioGroup v-model="dateSearchType" @on-change="changeSearchDate">
			  <Radio label="1">今日</Radio>
			  <Radio label="2">昨日</Radio>
			  <Radio label="3">最近七日</Radio>
			  <Radio label="4" disabled>自定义时间段</Radio>
			  <DatePicker
			    v-model="searchDateRange"
			    @click.native="dateChange"
			    type="daterange"
			    placement="bottom-end"
			    placeholder="订单日期"
			    style="width: 200px"
			  ></DatePicker>
			  

			  <Button type="success" @click="getData()">查询</Button>
			</RadioGroup>
			
			<Row class="margin-top-10 margin-bottom-10">
			  <Col span="24">
			    <RadioGroup v-model="orderStatus" type="button" size="large" @on-change="changeOrderStatus">
			        <Radio label="">全部</Radio>
			        <Radio label="0">待付款</Radio>
			        <Radio label="5">已付款</Radio>
					
			    </RadioGroup>
				<div class="f_r">
				  <Button  type="warning" @click="toCsv">导出</Button>
				</div>
			  </Col>
			</Row>
			
			
			
			<Table :columns="columns1" ref="Table"  :data="msgData.orders">
			</Table>
			<!-- <div class="totalCss">
				<div></div>
				<div></div>
				<div></div>
				<div>合计:10400</div>
				<div></div>
				<div></div>
				<div></div>
				<div></div>
				<div></div>
				<div>合计：10400</div>
			</div> -->
			<Spin size="large" fix v-if="spinShow"></Spin>
			
			<div style="margin: 10px 0;">
				<!-- <span>订单金额合计:999</span> -->
				<Page :total="msgData.total" :current="page" :page-size='pageSize' @on-change="switchPage">
					
					
				</Page>
				
			</div>
		</p>
    </Card>
  </div>
</template>

<script>
import table2excel from "@/libs/table2excel.js";
export default {
	components: {},
	data() {
	return {
		dateSearchType:"3",
		searchDateRange:"",
		page:1,
		msgData:{},
		columns1:[
			{title: '商户名称',key: 'merchantName'},
			{title: '订单名称',key: 'orderNo'},
			{title: '支付订单号',key: 'transactionId'},
			{title: '订单金额',key: 'actualAmount'},
			{title: '订单状态',key: 'orderStatus'},
			{title: '付款方式',key: 'paymentMethod'},
			{title: '下单时间',key: 'orderTime'},
			{title: '订单完成时间',key: 'paymentTime'},
			{title: '佣金金额',key: 'awardAmount'},
			{title: '收入',key: 'revenue'},
			
		],
		spinShow:true,
		orderStatus: "", //订单查询订单状态
		isTo:false,
		pageSize:50
	}
	},
	methods: {
		searchLeshuaOrder(page,orderStatus){
			if(page ==1){
				page=0;
			}else if(page > 1){
				page=page-1;
				page=page*10
			}
			let searchOptions = {
			  startIndex: page,
			  pageSize: this.pageSize,
			};
			let keyArray = [];
			// keyArray.push('MERCHANT');
			
			if(this.isTo){
				searchOptions.pageSize=100000;
			}
			
			console.log(this.common.getObject('MERCHANTUUID'))
			// searchOptions.merchantUuid = this.common.getObject('MERCHANTUUID');
			// searchOptions.merchantUuid = 'ff8080817b49529b017b495345830000';
			
			let dateType = this.dateSearchType;
			let startDate = null;
			let endDate = null;
			let currentDate = new Date();
			if (dateType == "1") {
			  startDate = currentDate;
			  endDate = currentDate;
			} else if (dateType == "2") {
			  currentDate.setDate(currentDate.getDate() - 1);
			  startDate = currentDate;
			  endDate = currentDate;
			} else if (dateType == "3") {
			  let tDate = new Date();
			  tDate.setDate(currentDate.getDate() - 7);
			  startDate = tDate;
			  endDate = currentDate;
			}else if (dateType == "4") {
				let t = this.searchDateRange;
				let startDateT = t[0];
				let endDateT = t[1];
				startDate = startDateT;
				endDate = endDateT;
			}
			searchOptions.orderDateStart = startDate;
			searchOptions.orderDateEnd = endDate;
			
			
			keyArray.push("ORDERDATE");
			
			if(orderStatus){
				searchOptions.orderStatus=orderStatus;
				keyArray.push("ORDERSTATUS");
			}
			
			
			searchOptions.keyArray = keyArray;
			// console.log(searchOptions)
			// console.log(this.common.request(searchOptions))
			
			this.$http.post("/admin/searchLeshuaOrder", this.common.request(searchOptions)).then((res)=>{
				console.log(res.data.body)
				if(res.data.body.status.errorDesc){
					console.log(res.data.body.status.errorDesc)
					this.msgData=res.data.body;
					this.spinShow=false;
					return;
				}
				this.msgData=res.data.body.data;
				var actualAmount=0;
				var revenue=0;
				var awardAmount=0;
				this.msgData.orders.forEach((obj)=>{
					actualAmount+=Number(obj.actualAmount)
					if(obj.revenue){
						revenue+=Number(obj.revenue)
					}
					if(obj.orderTime){
						obj.orderTime=this.Dateformat(obj.orderTime)
					}
					if(obj.paymentTime){
						obj.paymentTime=this.Dateformat(obj.paymentTime)
					}
					
					if(obj.awardAmount){
						awardAmount+=Number(obj.awardAmount)
					}
					
					if(obj.orderStatus == 5){
						obj.orderStatus="已完成"
					}else{
						obj.orderStatus="待付款"
					}
					
					if(obj.paymentMethod == 2){
						obj.paymentMethod ="支付宝";
					}else if(obj.paymentMethod == 1){
						obj.paymentMethod ="微信";
					}else{
						obj.paymentMethod ="未知";
					}
				})
				
				this.msgData.orders.push({
					'merchantName':'合计:',
					'actualAmount':actualAmount.toFixed(2),
					'revenue':revenue.toFixed(2),
					'awardAmount':awardAmount.toFixed(2),
				})
				
				if(this.isTo){
					var data = res.data.body.data;
					
					data.orders.forEach((obj)=>{
						if(obj.orderTime){
							obj.orderTime='\t'+obj.orderTime
						}
							
						if(obj.paymentTime){
							obj.paymentTime='\t'+obj.paymentTime
						}
						if(obj.orderNo){
							obj.orderNo='\t'+obj.orderNo
						}
						if(obj.transactionId){
							obj.transactionId='\t'+obj.transactionId
						}
						
					})
					
					this.$refs.Table.exportCsv({
						filename: "支付订单查询",
						columns: this.columns1,
						data: data.orders
					});
					this.isTo=false;
				}
				
				this.spinShow=false;
			});
			
		},
		toCsv(){
			this.isTo=true;
			this.searchLeshuaOrder();
		},
		changeSearchDate(){
			
		},
		dateChange(e){
			this.dateSearchType = "4";
		},
		getData(){
			this.spinShow=true;
			this.page=1;
			this.searchLeshuaOrder(0);
			
		},
		switchPage(e){
			this.spinShow=true;
			this.page=e;
			this.searchLeshuaOrder(this.page);
		},
		changeOrderStatus(e){
			this.searchLeshuaOrder(0,e);
		},
		Dateformat(sj){
			var sj=sj.split('T');
			return sj[0]+' '+sj[1].split('.')[0]
		}
	},
	mounted() {
		this.searchLeshuaOrder(0);
	}
};
</script>


<style type="text/css" lang="less">
	.totalCss{
		box-sizing: border-box;
		width: 100%;  height: 42px;
		div{
			display: inline-block;
			line-height: 42px;
			// height: 42px;
			width: 10%;
			font-size: 12px;
			// padding-left:8px;
		}
	}
</style>